package com.zhrj.exam.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.zhrj.exam.dto.ExcelDto;
import com.zhrj.exam.dto.QueryDto;
import com.zhrj.exam.entity.ZhrjAlarmInfo;
import com.zhrj.exam.service.IZhrjAlarmInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 事件表 前端控制器
 * </p>
 *
 * @author zhrj
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/zhrj/events")
public class ZhrjAlarmInfoController {

    @Autowired
    private IZhrjAlarmInfoService alarmInfoService;

    @PostMapping("/import")
    public void importFile(MultipartFile file) throws IOException {
        alarmInfoService.importFile(file);
    }

    @PostMapping("/statistic")
    public Map statistic() {
        return alarmInfoService.statistic();
    }

    @PostMapping("/month_date_count")
    public List<Map> monthDateCount(@RequestBody QueryDto dto) {
        return alarmInfoService.monthDateCount(dto.getDate());
    }

    /**
     * @param type 1(分页) 2(全量)
     */
    @PostMapping("/export")
    public void export(String type, Integer curr, Integer size, HttpServletResponse response) throws IOException {
        List<ZhrjAlarmInfo> list = alarmInfoService.export(type, curr, size);
        List<ExcelDto> excelDtoList = BeanUtil.copyToList(list, ExcelDto.class);
        EasyExcel.write(response.getOutputStream(), ExcelDto.class).sheet("模板").doWrite(excelDtoList);
    }
}
